//
//  HTAddMoodViewController.m
//  HappyTravel
//
//  Created by JiePan’s Mac on 2018/8/6.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "HTAddMoodViewController.h"
#import "HTModel.h"

@interface HTAddMoodViewController () <UITextViewDelegate> {
    
    float noteTextHeight;
    float pickerViewHeight;
}

@property (nonatomic, strong) NSMutableArray *ablumArray;

@end

@implementation HTAddMoodViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.barTintColor = ThemeColor;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:COLOR_RGB(255, 255, 255)}];
    [[UIApplication sharedApplication] setStatusBarStyle:(UIStatusBarStyleLightContent)];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:(UIStatusBarStyleDefault)];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"Add";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
}

#pragma mark - CustomMethod
- (void)setupUI {
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 30, 30);
    [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    leftBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [leftBtn addTarget:self action:@selector(actionOfLeftBtn) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 60, 30);
    [rightBtn setTitle:@"Publish" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [rightBtn addTarget:self action:@selector(actionOfRightBtn) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
    self.showInView = self.view;
    [self initPickerView];
    [self initViews];
}

- (void)initViews {
    
    _noteTextBackgroudView = [[UIView alloc]init];
    _noteTextBackgroudView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
    
    //TextView
    _noteTextView = [[UITextView alloc]init];
    _noteTextView.keyboardType = UIKeyboardTypeDefault;
    [_noteTextView setFont:[UIFont fontWithName:@"Heiti SC" size:16]];
    [_noteTextView setTextColor:[UIColor blackColor]];
    _noteTextView.delegate = self;
    
    //PlaceHolderLabel
    _placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 8, ScreenWidth-20, 30)];
    _placeholderLabel.text = @"Please enter your travel mood log...";
    _placeholderLabel.textColor = UIColorOfHex(0xCDCDCD);
    _placeholderLabel.enabled = NO;
    _placeholderLabel.font = [UIFont systemFontOfSize:14];
    _placeholderLabel.backgroundColor = [UIColor clearColor];
    [self.noteTextView addSubview:self.placeholderLabel];
    
    [self.view addSubview:self.noteTextBackgroudView];
    [self.view addSubview:self.noteTextView];
    
    [self updateViewsFrame];
}

- (void)updateViewsFrame {
    
    if (!noteTextHeight) {
        noteTextHeight = 100;
    }
    
    _noteTextBackgroudView.frame = CGRectMake(0, SafeAreaTopHeight, ScreenWidth, noteTextHeight);
    
    _noteTextView.frame = CGRectMake(15, SafeAreaTopHeight, ScreenWidth-30, noteTextHeight);
    
    //PhotoPicker
    [self updatePickerViewFrameY:_noteTextView.frame.origin.y + _noteTextView.frame.size.height];
}

- (void)textChanged {
    
    CGRect orgRect = self.noteTextView.frame;
    
    CGSize size = [self.noteTextView sizeThatFits:CGSizeMake(self.noteTextView.frame.size.width, MAXFLOAT)];
    
    orgRect.size.height = size.height+10;
    
    if (orgRect.size.height > 100) {
        noteTextHeight = orgRect.size.height;
    }else {
        noteTextHeight = 100;
    }
    
    [self updateViewsFrame];
}

#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    
    [self textChanged];
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    
    _noteTextView.text = textView.text;
    if (textView.text.length == 0) {
        _placeholderLabel.text = @"Please enter your travel mood log...";
    }else {
        _placeholderLabel.text = @"";
    }
}

#pragma maek - checkInput
- (BOOL)checkInput {
    
    if (self.noteTextView.text.length == 0) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Hint" message:@"Please enter your travel mood log" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *actionCacel = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:actionCacel];
        [self presentViewController:alertController animated:YES completion:nil];
        
        return NO;
    }else if (self.imageArray.count == 0) {
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Hint" message:@"Please add photos" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *actionCacel = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleCancel handler:nil];
        [alertController addAction:actionCacel];
        [self presentViewController:alertController animated:YES completion:nil];
        
        return NO;
    }
    
    return YES;
}

#pragma mark - touches events
- (void)actionOfLeftBtn {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)actionOfRightBtn {
    
    if ([self checkInput]) {
        
        NSArray *imageArr = self.arrSelected;
        
        NSMutableArray *imageDataArray = [NSMutableArray array];
        for (ALAsset *asset in imageArr) {
            UIImage *image = [[UIImage alloc] initWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
            NSData *imgData = UIImageJPEGRepresentation(image, 1.0);
            [imageDataArray addObject:imgData];
        }
        
        NSArray *tempArr = [HTSingleton readModelArrayForDocument:AblumModelPath];
        if (tempArr != nil) {
            [self.ablumArray addObjectsFromArray:tempArr];
        }

        HTModel *photos = [HTModel new];
        photos.imagePhotos = imageDataArray;
        photos.content = self.noteTextView.text;
        photos.currentTime = [HTSingleton getCurrentDay];
        [self.ablumArray addObject:photos];

        [HTSingleton saveModelDictionaryInDocument:self.ablumArray andPath:AblumModelPath];
        
        [self.navigationController popViewControllerAnimated:YES];

//        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Succeed" message:@"Publish success！" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *actionCacel = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//            [self removeSuperView];
//            [[NSNotificationCenter defaultCenter]postNotificationName:@"REFRESHDATA" object:nil];
//            [[NSNotificationCenter defaultCenter]postNotificationName:@"REFRESHALBUMDATA" object:nil];
//        }];
//        [alertController addAction:actionCacel];
//        [self presentViewController:alertController animated:YES completion:nil];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - getter
- (NSMutableArray *)ablumArray {
    if (!_ablumArray) {
        _ablumArray = [[NSMutableArray alloc] init];
    }
    return _ablumArray;
}






@end
